package com.example.audiostreaming.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.icu.lang.UCharacter;
import android.text.TextUtils;

import com.example.audiostreaming.Bean.UserBean;

public class UtilSP {
    /**
     * 当用户登陆时，利用SharePreferences 保存登录用户的用户标记（手机号码）
     */
    public static boolean saveUserPhone(Context context, String phone){
        SharedPreferences sp = context.getSharedPreferences(UtilConstants.SP_NAME_USER, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString(UtilConstants.SP_KEY_PHONE, phone);
        boolean result = edit.commit();
        return result;
    }

    /**
     * 当用户登陆时，利用SharePreferences 保存登录用户的用户标记（密码）
     */
    public static boolean saveUserPassword(Context context, String password){
        SharedPreferences sp = context.getSharedPreferences(UtilConstants.SP_NAME_USER, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString(UtilConstants.SP_KEY_PASSWORD, password);
        boolean result = edit.commit();
        return result;
    }

    /**
     * 验证是否存在已登录用户
     */

    public static boolean isLoginUser(Context context){
        boolean result = false;
        SharedPreferences sp = context.getSharedPreferences(UtilConstants.SP_NAME_USER, Context.MODE_PRIVATE);
        String phone = sp.getString(UtilConstants.SP_KEY_PHONE, "");
        String password = sp.getString(UtilConstants.SP_KEY_PASSWORD, "");
        String name = sp.getString(UtilConstants.SP_KEY_NAME, "");
        if (!TextUtils.isEmpty(phone)&&!TextUtils.isEmpty(password)) {
            result = true;
            UserBean.getUserBean().setTelephone(phone);
            UserBean.getUserBean().setPassword(password);
            UserBean.getUserBean().setName(name);
        }

        return result;
    }

    /**
     * 删除用户标记
     */
    public static boolean removeUser(Context context){
        SharedPreferences sp = context.getSharedPreferences(UtilConstants.SP_NAME_USER, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.remove(UtilConstants.SP_KEY_PHONE);
        edit.remove(UtilConstants.SP_KEY_PASSWORD);
        edit.remove(UtilConstants.SP_KEY_NAME);
        boolean result = edit.commit();
        return result;
    }



}
